package com.application.Activity;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.application.Controller.NetworkController;
import com.application.DTO.AccountDTO;
import com.application.Network.PingThread;
import com.application.Network.Protocol;


public class frmTest extends Activity {
	public static Runnable ra;
	public TextView text;
	Button btnStart,btnFinish;
	PingThread t;
public Handler handler;
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_test);
		String msg = "an/10;phu/11;truc/20";
		String[] m = msg.split(";");
		ArrayList<ArrayList<String>> item = new ArrayList<ArrayList<String>>();
		for (int i = 0; i < m.length; i++) {
			String[] s = m[i].split("/");
			ArrayList<String> sub = new ArrayList<String>();
			sub.add(s[0]);
			sub.add(s[1]);
			item.add(sub);
		}

//		StringBuffer buffer = new StringBuffer();
//		for (int i = 0; i < item.size(); i++) {
//			for (int j = 0; j < item.get(i).size(); j++) {
//				Log.d("phu", i + " " + j + " " + item.get(i).get(j));
//			}
//		}
//		try {
//			AssetManager as = getAssets();
//			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//			DocumentBuilder buider = factory.newDocumentBuilder();
//			Document doc = buider.parse(as.open("ListRule.xml"));
//		} catch (Exception e) {
//		}

		text = (TextView) findViewById(R.id.textView1);
		btnStart=(Button)findViewById(R.id.btnStart);
		btnFinish=(Button)findViewById(R.id.btnFinish);
		if(NetworkController._peer==null)
			NetworkController.init("MES", Protocol.PORT, new AccountDTO());
		handler=new Handler();
		Intent intent=new Intent(this,frmMatKetNoiActivity.class);
		
		
	
		NetworkController.thanhLapNhomIP="192.168.1.77:"+Protocol.PORT;
		
	btnStart.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				t.setRunning(false);
				NetworkController.thanhLapNhomIP="";
			}
		});
		btnFinish.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				frmTest.this.finish();
				System.exit(0);
			}
		});
	}
	@Override
	public void onDestroy(){
		super.onDestroy();
	}
	@Override
	public void onResume(){
		
		super.onResume();
		Log.d("Phu", "On resume");
//		NetworkController.setStated(true);
//		if(PingThread.getInstance("test").getState()==Thread.State.NEW){
//			PingThread.setRunning(true);
//			PingThread.getInstance("test").start();
//			Log.d("Phu","start ping in test");
//		}else{
//			PingThread.setRunning(true);
//			Log.d("Phu","setRunning");
//		}
		setTitle(NetworkController._peer.getAddressPeer());
		NetworkController.setStated(true);
		t=new PingThread("Test");
		t.start();
	}
}
